package c_007;

import java.util.ArrayList;
import java.util.List;

/**
 * volatile并不能保证多个线程共同修改runnning变量时所带来不一致的问题,也就是说volatile不能替代synchronized
 * 运行下面程序,并分析结果
 */
public class T01 {

    volatile int count = 0;
    void m() {
        for(int i = 0; i < 10000; i++){
            count++;
        }
    }

    public static void main(String[] args) {
        T01 t = new T01();
        List<Thread> threads = new ArrayList<>();
        for(int i = 0; i < 10; i++){
            threads.add(new Thread(t::m, "thread-" + i));
        }

        threads.forEach((o) -> o.start());

        threads.forEach((o) -> {
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(t.count);
    }
}
